<%--
  Created by IntelliJ IDEA.
  User: Jia_ly
  Date: 2016/12/21
  Time: 15:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path;
    request.setAttribute("basePath", basePath);
%>
<html>
<%@ include file="../frame/head.jsp" %>
<body>
<section>
    <%@include file="../frame/top.jsp" %>
    <section class="boxMain">
        <section class="hbox stretch">
            <%@include file="../frame/aside.jsp" %>
            <section>
                <header>
                    <h3 style="color: #0b6cbc;text-align: center">个人信息</h3>
                </header>
                <hr>
                <div>
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <tbody style="text-align:center">
                            <tr>
                                <td><strong>创建时间:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="createTime" type="text" class="form-control" readonly
                                               value=${userMassage.createTime}>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>用户名:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="userName" type="text" class="form-control" readonly
                                               value=${userMassage.userName}>
                                    </div>
                                </td>
                            </tr>

                            <tr>
                                <td><strong>姓名:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="Name" type="text" class="form-control" readonly
                                               value=${userMassage.tureName}>
                                    </div>
                                </td>
                            </tr>

                            <tr>
                                <td><strong>角色:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="role" type="text" class="form-control" readonly
                                               value=${userMassage.roleName}>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>邮箱:</strong></td>
                                <td>
                                    <div class="col-xs-4">
                                        <input id="mail" type="text" class="form-control" readonly
                                               value=${userMassage.mail}>
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div align="center">
                    <button id="save" class="btn btn-s-md btn-primary">保存</button>
                    &nbsp;&nbsp;
                    <button id="edit" class="btn btn-s-md btn-danger" onclick="edit()">编辑</button>
                </div>
                </div>
            </section>
        </section>
    </section>
</section>
</body>
<script>
    /*如果没有登录，则跳转到登陆页面*/
    if (<%=session.getAttribute("userName") == null%>) {
        location.href = "${basePath}";
    }
</script>
<%--编辑按钮--%>
<script>
    function edit() {
        var txt = document.getElementsByTagName("input");
        for (i = 0; i < txt.length; i++) {
            if ((i == 2) || (i == 4)) {
                txt[i].readOnly = false;
            }
        }
    };
</script>

<%--保存按钮--%>
<script>
    function judge() {
        var username = $("#userName").val();
        var name = $("#Name").val();
        var mail = $("#mail").val();
        if ((username == "") || (name == "") || (mail == "")) {
            layer.alert("有内容没有填写");
            edit();
            return false;
        }
        else {
            return true;
        }
    }

    $("#save").click(function () {
        var email = /^([a-zA-Z0-9_\.-]+)@(([a-zA-Z0-9_-]+)\.)+[a-zA-Z]{2,3}$/;
        var myMail = $("#mail").val();
        var txt = document.getElementsByTagName("input");
        for (i = 0; i < txt.length; i++) {
            if ((i == 2) || (i == 4)) {
                txt[i].readOnly = true;
            }
        }
        if (judge() && email.test(myMail)) {
            $.ajax({
                type: 'POST',
                asyn: false,
                url: "${basePath}/user/updateUserMessage",
                data: {
                    username: $("#userName").val(),
                    name: $("#Name").val(),
                    mail: $("#mail").val()
                },
                dataType: 'JSON',
                success: function (data) {
                    if (true == data) {
                        layer.alert("操作成功");
                    } else {
                        layer.alert("操作失败");
                    }
                }
            });
        }
        else {
            layer.alert("邮箱格式错误");
            edit();
        }
    });
</script>
</html>
